package com.label.grade.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.label.grade.entity.Do.ExtracurricularScoreSummary;
import com.label.grade.entity.Vo.ExtracurricularOverallScoreVo;
import com.label.grade.entity.Vo.ExtracurricularScoreVo;
import com.label.grade.mapper.ExtracurricularScoreSummaryMapper;
import com.label.grade.service.ExtracurricularScoreSummaryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;

/**
* @author Administrator
* @description 针对表【EXTRACURRICULAR_SCORE_SUMMARY(学生成绩-二课成绩汇总信息表)】的数据库操作Service实现
* @createDate 2023-07-06 16:35:55
*/
@Service
public class ExtracurricularScoreSummaryServiceImpl extends ServiceImpl<ExtracurricularScoreSummaryMapper, ExtracurricularScoreSummary>
    implements ExtracurricularScoreSummaryService {

    @Resource
    private ExtracurricularScoreSummaryMapper extracurricularScoreSummaryMapper;

    @Override
    public List<ExtracurricularOverallScoreVo> getExtracurricularOverallScore() {

        List<ExtracurricularOverallScoreVo> extracurricularOverallScoreList = extracurricularScoreSummaryMapper.listExtracurricularOverallScore();
        return extracurricularOverallScoreList;
    }

    @Override
    public ExtracurricularScoreVo getExtracurricularScore(String studentCode) {

        //1 获取基础信息和排名，通过sql直接排序
        ExtracurricularScoreVo extracurricularScoreVo = extracurricularScoreSummaryMapper.selExtracurricularScore(studentCode);
        //2 获取所有维度数据
        ExtracurricularScoreSummary extracurricularScoreSummary = extracurricularScoreSummaryMapper.selectOne(new QueryWrapper<ExtracurricularScoreSummary>().lambda().eq(ExtracurricularScoreSummary::getStudentCode,studentCode));
        //3 维度数据填充
        //3.1 第一维度
        ExtracurricularScoreVo.DimensionOne dimensionOne = extracurricularScoreVo.new DimensionOne();
        dimensionOne.setAcademicTechnology(String.valueOf(extracurricularScoreSummary.getAcademicTechnology()));
        dimensionOne.setPracticalService(String.valueOf(extracurricularScoreSummary.getPracticalService()));
        dimensionOne.setIdealsBeliefs(String.valueOf(extracurricularScoreSummary.getIdealsBeliefs()));
        dimensionOne.setLiteratureArt(String.valueOf(extracurricularScoreSummary.getLiteratureArt()));
        dimensionOne.setPhysicalFitness(String.valueOf(extracurricularScoreSummary.getPhysicalFitness()));
        dimensionOne.setSocialWork(String.valueOf(extracurricularScoreSummary.getSocialWork()));
        //3.2 第二维度
        ExtracurricularScoreVo.DimensionTwo dimensionTwo = extracurricularScoreVo.new DimensionTwo();
        dimensionTwo.setAwardsHonors(String.valueOf(extracurricularScoreSummary.getAwardsHonors()));
        dimensionTwo.setActivitiesCompetitions(String.valueOf(extracurricularScoreSummary.getActivitiesCompetitions()));
        dimensionTwo.setStudentOrganization(String.valueOf(extracurricularScoreSummary.getStudentOrganization()));
        //3.3 第三维度
        ExtracurricularScoreVo.DimensionThree dimensionThree = extracurricularScoreVo.new DimensionThree();
        dimensionThree.setCollegeLevel(String.valueOf(extracurricularScoreSummary.getCollegeLevel()));
        dimensionThree.setNationalLevel(String.valueOf(extracurricularScoreSummary.getNationalLevel()));
        dimensionThree.setProvincialLevel(String.valueOf(extracurricularScoreSummary.getProvincialLevel()));
        dimensionThree.setUniversityLevel(String.valueOf(extracurricularScoreSummary.getUniversityLevel()));
//        //4 维度数据填充至Vo
//        extracurricularScoreVo.setDimensionOne(dimensionOne);
//        extracurricularScoreVo.setDimensionTwo(dimensionTwo);
//        extracurricularScoreVo.setDimensionThree(dimensionThree);

//        extracurricularScoreVo.setTotalGrade(sumProperties(extracurricularScoreSummary) - extracurricularScoreSummary.getOverallGrade() );



        return extracurricularScoreVo;
    }

    public static double sumProperties(Object entity) {
        double sum = 0.0;

        // 获取实体类的所有字段
        Field[] fields = entity.getClass().getDeclaredFields();

        // 遍历字段并累加求和
        for (Field field : fields) {
            // 设置字段为可访问，以便获取其值
            field.setAccessible(true);

            // 判断字段类型并进行累加
             if (field.getType() == double.class || field.getType() == Double.class) {
                try {
                    Double value = (Double) field.get(entity);
                    sum += value;
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            // 其他数据类型可以根据需要进行扩展

            // 恢复字段的访问性为默认状态
            field.setAccessible(false);
        }

        return sum;
    }
}




